package models

import (
	"context"
	"database/sql"
	"fmt"
	"github.com/zeromicro/go-zero/core/stores/sqlx"
	"strings"
)

func (m *defaultMessageStatusModel) BatchUpdateByMsgID(ctx context.Context, msgId []int64, user_id, status int64) error {
	var msgStrList []string
	for _, id := range msgId {
		msgStrList = append(msgStrList, fmt.Sprintf("%v", id))
	}
	msgStr := strings.Join(msgStrList, ",")

	query := fmt.Sprintf("update %s set `read_status` = %d where `msg_id` in (%s)", m.table, status, msgStr)
	if user_id != 0 {
		query += fmt.Sprintf(" and `user_id` = %d", user_id)
	}
	_, err := m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
		return conn.ExecCtx(ctx, query)
	})
	return err
}
